package com.michael.study.listview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 * Description:
 *
 * Created by liuguoquan on 2020/7/26 23:19.
 */
class HeaderItemDecoration(val context: Context) : RecyclerView.ItemDecoration() {

    val mDrawable = context.resources.getDrawable(android.R.color.holo_red_dark)
    val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    init {
        mPaint.color = Color.RED
    }


    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            var  index =  parent.getChildAdapterPosition(child)
            if (index % 4 == 0) {
                mDrawable.setBounds(0, child.top, child.right, child.top + 20)
                mDrawable.draw(c)
            } else {
                mDrawable.setBounds(0, parent.top, child.right, parent.top + 20)
                mDrawable.draw(c)
            }
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {

        outRect.left = 0
        outRect.top = 0
        outRect.right = 0
        outRect.bottom = 1

    }
}